package com.resource.base.intercept;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.resource.project.common.HrUtils;
import com.resource.project.entity.system.Hr;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyAbstractSqlParserHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (null==authentication){
            return;
        }
        if (authentication.getPrincipal() instanceof String){
            return;
        }
        Hr hr = HrUtils.getCurrentHr();
        this.setFieldValByName("createdDate", LocalDateTime.now(), metaObject);
        this.setFieldValByName("createdId", hr.getId(), metaObject);
        this.setFieldValByName("createdName", hr.getName(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }

}
